<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use app\admin\model\Pic;
use think\Db;
use think\db\Where;
use think\Model;

class ArticleOrder extends Model {
    protected $autoWriteTimestamp = false;
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*',$out=false){
        $list = $this
            ->field($filed,$out)
            ->with(['article'=>function($query){
                $query->field('id,title,logo,shop_type,contact,real_name,location,is_vip,is_vip');
            }])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        foreach ($list as $k => &$v){
            $v['user'] = (new Member())->field('id,account,avatar,nickname')->find($v['uid']);
        }
        return $list;
    }

    /**
     * @param array $where
     * @param string $order
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllList($where=[],$order='create_time desc'){
        $list = $this
            ->field('create_time',true)
            ->with(['shop'=>function($query){
                $query->field('id,title,shop_type,contact,real_name,location,is_vip,is_vip');
            }])
            ->where($where)
            ->order($order)
            ->select();
        return $list;
    }

    /**
     * @param $uid
     * @param string $title
     * @param string $aid
     * @param string $information
     * @param string $totals
     * @param string $discount
     * @param string $out
     * @return bool
     */
    public function addNew($uid,$title='',$aid='',$information='',$totals='',$discount='',&$out=''){
        $order_id = build_order_no1('ati_');
        $data = [
            'uid'=>$uid,
            'aid'=>$aid,
            'title'=>$title,
            'order_id'=> $order_id,
            'information'=>$information,
            'totals'=>$totals,
            'discount'=>$discount,
            'status'=>1,
            'create_time'=>time(),
        ];
        $valid = new \app\first\validate\ArticleOrder();
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        if(!$id = $this->insertGetId($data)){
            $out = '添加失败';
            return false;
        }
        $out = [
            'id'=>$id,
            'order_id'=>$order_id
        ];
        return true;
    }


    /**
     * @param int $id
     * @param string $filed
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id=0,$filed=''){
        $filed = $filed?$filed:'update_time';
        $info = $this->field($filed,true)
            ->with(['shop'=>function($query){
                $query->field('id,title,logo,shop_type,contact,real_name,location,is_vip,is_vip');
            }])
            ->find($id);
        if($info){
            $info['content'] = json_decode($info['content'],true);
            $res['user'] = (new Member())->field('id,account,avatar,nickname')->find($info['uid']);
        }
        return $info;
    }

    /**
     * @param $order_id
     * @param $transaction_id
     * @param $total_fee
     * @param int $type 1微信,2支付宝
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function confirm($order_id,$transaction_id,$total_fee,$type=1){
        $info = $this->where('order_id','eq',$order_id)->find();
        if(!$info){
            return false;
        }
        if($info['status']!=1){
            return false;
        }
        $totals = $type==1?($total_fee/100):$total_fee;
        $info->pay_time =time();
        $info->pay_type = $type;
        $info->actual_payment = $totals;
        $info->transaction_id =$transaction_id;
        if($info['totals']!=$totals){
            $info->status = 3;
            $info->reason = '金额不正确';
        }else{
            $info->status = 2;
        }
        $art = new Article();
        $_info = $art->find($info['aid']);
        if(!$_info){
            return false;
        }
        Db::startTrans();
        $_info->is_payed = 1;
        $result = $_info->save();
        $result1 = $info->save();
        if($result && $result1){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }

    public function article(){
        return $this->hasOne('article','id','aid');
    }
}